﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGame.Networking.Clientside.ServerClientPackets
{
    class CreateEntityPacket : ClientPacket
    {
        private string ID;
        private int X;
        private int Y;
        private int Angle;
        private string Texture;
        private int R;
        private int G;
        private int B;

        public override ServerClientNet GetID()
        {
            return ServerClientNet.EntityCreate;
        }

        public override bool Validate(Lidgren.Network.NetIncomingMessage msg)
        {
            this.ID = msg.ReadString();
            this.X = msg.ReadInt32();
            this.Y = msg.ReadInt32();
            this.Angle = msg.ReadInt32();
            this.Texture = msg.ReadString();
            this.R = msg.ReadInt32();
            this.G = msg.ReadInt32();
            this.B = msg.ReadInt32();

            if (!this.ValidateString(this.ID))
            {
                return false;
            }
            if (!this.ValidateInt(this.X))
            {
                return false;
            }
            if (!this.ValidateInt(this.Y))
            {
                return false;
            }
            if (!this.ValidateInt(this.Angle))
            {
                return false;
            }
            if (!this.ValidateString(this.Texture))
            {
                return false;
            }
            if (!this.ValidateInt(this.R))
            {
                return false;
            }
            if (!this.ValidateInt(this.G))
            {
                return false;
            }
            if (!this.ValidateInt(this.B))
            {
                return false;
            }

            return true;
        }

        public override void Handle()
        {
            Singletons.NetworkEntity.AddNew(this.ID, this.X, this.Y, this.Angle, this.Texture, this.R, this.G, this.B);
        }

        public override void Handle(Lidgren.Network.NetIncomingMessage message)
        {
            this.Validate(message);
            this.Handle();
        }
    }
}
